package com.ethan.springcloud.helloserviceclient;

import com.ethan.springclod.api.vo.User;
import com.ethan.springcloud.helloserviceclient.service.HelloFeignService;
//import com.ethan.springcloud.helloserviceclient.service.HelloRemoteApi;
import com.ethan.springcloud.helloserviceclient.service.HelloRemoteApi;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author daifei
 * @date 2019/3/22
 */
@RestController
@Slf4j
public class HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    HelloFeignService helloFeignService;

    @Autowired
    HelloRemoteApi helloRemoteApi;


    /**
     * 坑点： 使用 服务名调用服务，必须@LoadBalanced 注解
     *
     * @param name
     * @return
     */
    @GetMapping("hello")
    public String hello(String name) {
        String responseEntity = restTemplate.getForObject("http://HELLOSERVICE/hello?name={1}", String.class, name);
        log.info("hello service :{}", responseEntity);
        return responseEntity;
    }

    @GetMapping("hello1")
    public String hello2(String name) {
        if (StringUtils.isEmpty(name)) {
            return helloFeignService.hello();
        } else {
            return helloFeignService.helloName(name);
        }
    }
//
    @GetMapping("hello2")
    @HystrixCommand(fallbackMethod = "fallback")
    public String hello3() {
        User user = new User();
        user.setAge(33);
        user.setName("ethan");
        return helloFeignService.hello2(user);
    }

    public String fallback(){
        log.info("this is fallback method!!!");
        return "error***this is fallback method!!!";
    }

    /**
     * remoteApi 调用
     *
     * @return
     */
    @GetMapping("hello5")
    public String hello5() {
        com.ethan.springclod.api.vo.User user = new com.ethan.springclod.api.vo.User();
        user.setAge(26);
        user.setName("honey");
        log.info("remoteApi 调用！");
        return helloRemoteApi.hello2(user);
    }


}
